package com.netstar.xmlparser;

import java.io.Serializable;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.netstar.util.EvtLog;

public class ServerChannelHelper {

	public static class ServerChannelItem implements Serializable {

		private static final long serialVersionUID = 2645144326351775846L;

		public String day;
		public String imei;
		public String phone;
		public long dataMobile;
		public long timeMobile;
		public long dataCMCC;
		public long timeCMCC;
		public long dataUnicom;
		public long timeUnicom;
		public long dataTelecom;
		public long timeTelecom;
		public long dataWifi;
		public long timeWifi;
	}

	public static ArrayList<ServerChannelItem> parse(String xml) {
		return new ContentParser().parse(xml);
	}

	private static class ContentParser extends DefaultHandler {

		private static final String TAG = "ServerChannelHelper.ContentParser";

		private ArrayList<ServerChannelItem> mItems = new ArrayList<ServerChannelItem>();

		private ServerChannelItem mItem;

		public ArrayList<ServerChannelItem> parse(String xml) {
			if (null == xml || xml.length() == 0) {
				return mItems;
			}
			SAXParserFactory spf = SAXParserFactory.newInstance();
			try {
				StringReader sReader = new StringReader(xml);
				SAXParser parser = spf.newSAXParser();
				parser.parse(new InputSource(sReader), this);
			} catch (Exception e) {
				EvtLog.e(TAG, e.getMessage());
			}
			return mItems;
		}

		@Override
		public void startDocument() throws SAXException {
			EvtLog.i(TAG, "startDocument");
		}

		@Override
		public void endDocument() throws SAXException {
			EvtLog.i(TAG, "endDocument");
		}

		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			if (localName.equals("datas")) {
				mBuilder = new StringBuilder();
			} else if (localName.equals("item")) {
				mItem = new ServerChannelItem();
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			final String value = mBuilder.toString().trim();
			if (localName.equals("item")) {
				mItems.add(mItem);
			} else if (localName.equals("cdate")) {
				mItem.day = value;
			} else if (localName.equals("imei")) {
				mItem.imei = value;
			} else if (localName.equals("mobilephone")) {
				mItem.phone = value;
			} else if (localName.equals("telwificount")) {
				try {
					mItem.dataTelecom = Long.parseLong(value);
				} catch (NumberFormatException e) {
					EvtLog.e(TAG, "unexpected value " + value + " for dataTelecom");
				}
			} else if (localName.equals("telwifitime")) {
				try {
					mItem.timeTelecom = Long.parseLong(value);
				} catch (NumberFormatException e) {
					EvtLog.e(TAG, "unexpected value " + value + " for timeTelecom");
				}
			} else if (localName.equals("mobwificount")) {
				try {
					mItem.dataCMCC = Long.parseLong(value);
				} catch (NumberFormatException e) {
					EvtLog.e(TAG, "unexpected value " + value + " for dataCMCC");
				}
			} else if (localName.equals("mobwifitime")) {
				try {
					mItem.timeCMCC = Long.parseLong(value);
				} catch (NumberFormatException e) {
					EvtLog.e(TAG, "unexpected value " + value + " for timeCMCC");
				}
			} else if (localName.equals("uniwificount")) {
				try {
					mItem.dataUnicom = Long.parseLong(value);
				} catch (NumberFormatException e) {
					EvtLog.e(TAG, "unexpected value " + value + " for dataUnicom");
				}
			} else if (localName.equals("uniwifitime")) {
				try {
					mItem.timeUnicom = Long.parseLong(value);
				} catch (NumberFormatException e) {
					EvtLog.e(TAG, "unexpected value " + value + " for timeUnicom");
				}
			} else if (localName.equals("privatewificount")) {
				try {
					mItem.dataWifi = Long.parseLong(value);
				} catch (NumberFormatException e) {
					EvtLog.e(TAG, "unexpected value " + value + " for dataWifi");
				}
			} else if (localName.equals("privatewifitime")) {
				try {
					mItem.timeWifi = Long.parseLong(value);
				} catch (NumberFormatException e) {
					EvtLog.e(TAG, "unexpected value " + value + " for timeWifi");
				}
			} else if (localName.equals("gprswificount")) {
				try {
					mItem.dataMobile = Long.parseLong(value);
				} catch (NumberFormatException e) {
					EvtLog.e(TAG, "unexpected value " + value + " for dataMobile");
				}
			} else if (localName.equals("gprswifitime")) {
				try {
					mItem.timeMobile = Long.parseLong(value);
				} catch (NumberFormatException e) {
					EvtLog.e(TAG, "unexpected value " + value + " for timeMobile");
				}
			} else {
				EvtLog.i(TAG, "unknown tag " + localName);
			}
		}

		private StringBuilder mBuilder;

		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
			mBuilder.setLength(0);
			mBuilder.append(ch, start, length);
		}
	}

}
